package com.showde.shoppingmall.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.kim.androidbase.tools.DigestUtils;
import com.kim.file.FileUtils;
import com.kim.file.SDCardUtils;
import com.kim.net.NetCallback;
import com.kim.net.NetEntity;
import com.kim.net.impl.NetProcessProxy;

public class DrawableUtils {

	public static final String IMG_CACHE_DIR = "com.kim.rssreader.dir";

	public interface ImageSaveCallback {
		void onSuccess(String localPath);

		void onError(String errorMsg);
	}

	/**
	 * 获取图片保存地址
	 * 
	 * @param imgUrl
	 * @return
	 */
	public static final String getImageSavePath(String imgUrl) {
		return SDCardUtils.fileDir + "/" + IMG_CACHE_DIR + "/"
				+ generateFileDir(imgUrl) + "/" + generateFileName(imgUrl);
	}

	/**
	 * 获取图片保存名称
	 * 
	 * @param imgUrl
	 * @return
	 */
	public static final String generateFileName(String imgUrl) {
		return DigestUtils.shaHex(imgUrl);
	}

	/**
	 * 获取图片保存目录
	 * 
	 * @param imgUrl
	 * @return
	 */
	public static final String generateFileDir(String imgUrl) {
		return DigestUtils.shaHex(imgUrl).substring(0, 2);
	}

	/**
	 * 保存图片
	 * 
	 * @param savePath
	 * @param netUrl
	 * @param inputStream
	 * @return
	 */
	public static final boolean saveInputStream2Drawable(String savePath,
			String netUrl, InputStream inputStream) {
		try {
			File file = new File(savePath);

			if (file.exists()) {
				return true;
			}

			file.getParentFile().mkdirs();
			file.createNewFile();

			OutputStream outputStream = new FileOutputStream(file);

			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			if (bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
				bitmap.recycle();
				return false;
			}
			bitmap.compress(CompressFormat.JPEG, 80, outputStream);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

		return false;
	}

	public static final void saveImage(final String imageUrl,
			final ImageSaveCallback imageSaveCallback) {

		/**
		 * 若文件已存在，则直接返回
		 */
		if (FileUtils.fileExist(getImageSavePath(imageUrl))) {
			return;
		}

		NetProcessProxy.doGet(imageUrl, new NetCallback() {

			@Override
			public void onSuccess(int httpResponseCode,
					InputStream inputStream, NetEntity netEntity, boolean gzip) {
				try {
					if (inputStream == null || inputStream.available() == 0) {
						imageSaveCallback.onError(null);
						return;
					}
					boolean saveResult = saveInputStream2Drawable(
							getImageSavePath(imageUrl), imageUrl, inputStream);
					if (saveResult) {
						imageSaveCallback.onSuccess(getImageSavePath(imageUrl));
					} else {
						imageSaveCallback.onError(null);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onError(int httpResponseCode, String errorMsg) {

			}

			@Override
			public void onOtherResponseCode(int httpResponseCode,
					InputStream inputStream, NetEntity netEntity, boolean gzip) {
				
			}
		});

	}

	/**
	 * 根据网络地址从本地加载图片
	 * 
	 * @param imageNetUrl
	 * @return
	 */
	public static Drawable loadDrawableByNetUrl(String imageNetUrl) {
		String localPath = getImageSavePath(imageNetUrl);
		return loadDrawableByLocalPath(localPath);
	}

	/**
	 * 根据本地地址加载缓存图片
	 * 
	 * @param localPath
	 * @return
	 */
	public static Drawable loadDrawableByLocalPath(String localPath) {
		try {
			InputStream inputStream = new FileInputStream(new File(localPath));
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
			return (Drawable) bitmapDrawable;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
		return null;
	}

	public static final File loadImageCache(Context context, String imageUrl) {
		return null;
	}

	public static final void imageCache2SDCard(String imageUrl) {

	}

	/**
	 * 根据图片网络地址删除图片
	 * 
	 * @param imageNetUrl
	 */
	public static void deleteDrawableByNetUrl(String imageNetUrl) {
		deleteDrawableByLocalPath(getImageSavePath(imageNetUrl));
	}

	/**
	 * 根据图片本地地址删除图片
	 * 
	 * @param imageLocalPath
	 */
	public static void deleteDrawableByLocalPath(String imageLocalPath) {
		File file = new File(imageLocalPath);
		if (!file.exists()) {
			return;
		}
		file.delete();
	}
}
